package com.siwei.mes.service.experiment.impl;

import com.siwei.mes.common.util.ScriptUtils;
import com.siwei.mes.entity.experiment.*;
import com.siwei.mes.mapper.experiment.*;
import com.siwei.mes.service.experiment.ExperimentCheckRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账检测记录相关
 *
 * @Description: 试验台账检测记录相关
 * @Version: 1.0
 * @author: chenmaolin
 * @date: 2024/1/6 20:49
 */
@Slf4j
@Service
public class ExperimentCheckRecordServiceImpl implements ExperimentCheckRecordService {
    @Resource
    private ExperimentCheckRecordMapper experimentCheckRecordMapper;
    @Resource
    private ExperimentQuickRecordMapper experimentQuickRecordMapper;
    @Resource
    private CheckQuickConfigMapper checkQuickConfigMapper;

    @Resource
    private CheckBatchConfigMapper checkBatchConfigMapper;
    @Resource
    private ExperimentBatchRecordMapper experimentBatchRecordMapper;

    @Override

    public List<ExperimentCheckRecord> selectByExperimentId(Long experimentId) {
        return experimentCheckRecordMapper.getList(new ExperimentCheckRecord().setExperimentId(experimentId));
    }

    @Override
    public void update(ExperimentCheckRecord experimentCheckRecord) {
        Integer checkType = experimentCheckRecord.getCheckType();// 检测类型 1-快检 2-批检
        String resultValue = experimentCheckRecord.getResultValue();// 检测值
        if (checkType == 1) {
            ExperimentQuickRecord experimentQuickRecord = experimentQuickRecordMapper.loadById(experimentCheckRecord.getId());
            assertTrue(experimentQuickRecord == null, "快检检测记录不存在");
            CheckQuickConfig quickConfig = checkQuickConfigMapper.loadById(experimentQuickRecord.getQuickId());
            assertTrue(quickConfig == null, "快检配置信息不存在");
            experimentQuickRecord.setResultValue(resultValue);
            experimentQuickRecord.setImg(experimentCheckRecord.getImg());
            experimentQuickRecord.init(false);
            /**判断是否合格*/
            Boolean compareFormula = ScriptUtils.compareFormula(quickConfig.getQualifiedCheckText(), resultValue);
            if (compareFormula) {
                experimentQuickRecord.setIsQualified(1);
            } else {
                experimentQuickRecord.setIsQualified(2);
            }
            experimentQuickRecordMapper.updateByPrimaryKeySelective(experimentQuickRecord);
        } else if (checkType == 2) {
            ExperimentBatchRecord experimentBatchRecord = experimentBatchRecordMapper.loadById(experimentCheckRecord.getId());
            assertTrue(experimentBatchRecord == null, "批检检测记录不存在");
            CheckBatchConfig batchConfig = checkBatchConfigMapper.loadById(experimentBatchRecord.getBatchId());
            assertTrue(batchConfig == null, "批检配置信息不存在");
            experimentBatchRecord.setResultValue(resultValue);
            experimentBatchRecord.setImg(experimentCheckRecord.getImg());
            experimentBatchRecord.init(false);
            /**判断是否合格*/
            /**先判断是否合格*/
            Boolean compareFormula = ScriptUtils.compareFormula(batchConfig.getQualifiedCheckText(), resultValue);
            if (compareFormula) {
                experimentBatchRecord.setIsQualified(1);
            } else {
                experimentBatchRecord.setIsQualified(2);
            }
            experimentBatchRecordMapper.updateByPrimaryKeySelective(experimentBatchRecord);
        }
    }
}
